<template>
  <div class="login">
    <el-form ref="form" class="form" :model="form" :rules="rules">
      <el-form-item>
        <img src="@/assets/common/login-logo.png" alt="">
      </el-form-item>
      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          prefix-icon="el-icon-user-solid"
          @keyup.enter.native="submit"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          prefix-icon="el-icon-lock"
          show-password
          @keyup.enter.native="submit"
        />
      </el-form-item>
      <el-form-item>
        <!-- 登录
          1：定义登录api
          2:导入与调用，
          3：调用完成保存token到vuex并同步到cookie
          -->

        <el-button type="primary" class="btn" @click="submit">登录</el-button>
      </el-form-item>
      <el-form-item>
        <div ref="tip" class="tip">用户名:13800000002,密码:123456</div>
        <!-- <Test ref="test" @click.native="testClick" /> -->
        <!-- native修饰符：作用让组件标签可以使用原生的方法 -->
        <!-- <Test>
          <template v-slot="scope">
            {{ scope.num }}
          </template>
        </Test> -->
      </el-form-item>
    </el-form>
    <!-- ref功能
    1：用于原生标签获取dom
       原生标签  ref="属性名"
       访问：this.$refs.属性名====原生标签的dom
    2:用于组件标签获取组件的实例对象
      组件标签  ref="属性名"
      访问组件的this(实例对象):this.$refs.属性名====组件的实例对象
     -->
  </div>
</template>
<script>
// import Test from './components/test.vue'
// import { sysLogin } from '@/api/user.js'
export default {
  components: {
    // Test
  },
  data() {
    return {
      form: {
        mobile: '13800000002', //	string	必须		手机号 必填加手机号验证
        password: '123456' //	string	必须		密码  6-12位
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rules, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('请正确输入手机号'))
            },
            trigger: 'change'
          }
        ], //	string	必须		手机号
        password: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 6,
            max: 12,
            message: '请输入6-12位密码',
            trigger: 'change'
          }
        ] //	string	必须		密码
      }
    }
  },
  methods: {
    testClick() {
      console.log('testClick')
    },
    submit() {
      // console.log(this.$refs.tip)
      // console.log(this.$refs.test)
      // this.$refs.test.validate()
      this.$refs.form.validate(async(result) => {
        // result:true:验证成功
        //        false:验证失败
        if (result) {
          // const res = await sysLogin(this.form)
          // this.$store.commit('user/setToken', res.data)
          // console.log(res)
          // console.log(1)
          // console.log(2)
          // this.$store
          //   .dispatch('user/toLogin', this.form)
          //   .then(() => {
          //     this.$message.success('登录成功')
          //   })
          //   .catch((error) => {
          //     console.log(error)
          //   })
          // try {
          await this.$store.dispatch('user/toLogin', this.form)
          this.$message.success('登录成功')
          this.$router.push('/')
          // } catch (error) {
          //   console.log(error)
          // }
        }
        // console.log(result)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
// scoped看得到的就管得到，看不到的就管不到
// 子组件只能管到最外层
// 希望管到内部  >>>  /deep/  ::v-deep
.login {
  width: 100%;
  height: 100%;
  background: url('~@/assets/common/login.jpg') no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .btn {
    width: 100%;
    height: 60px;
    background-color: #407ffe;
  }
  .tip {
    text-align: center;
    color: #fff;
  }
  ::v-deep .el-input__inner {
    height: 52px;
  }
  ::v-deep .el-form-item__error {
    color: #fff;
  }
}
</style>
